#![allow(clippy::too_many_arguments)]

use crate::service::DATE_FORMAT;
use crate::SQLX;
use anyhow::anyhow;
use chrono::Local;
use serde::{Deserialize, Serialize};
use sqlx::sqlite::SqliteQueryResult;
use sqlx::FromRow;

#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct SysOrg {
    pub org_id: Option<i64>,
    pub org_name: Option<String>,
    pub create_time: Option<String>,
    pub remark: Option<String>,
}

impl SysOrg {
    pub async fn add_org(self) -> anyhow::Result<SqliteQueryResult> {
        let sqlx = SQLX.get().ok_or(anyhow!("db error"))?;
        let mut db = sqlx.acquire().await?;
        let sql = r#"INSERT INTO sys_org(org_name,create_time,remark)values (?,?,?)"#;
        let create_time = Local::now().format(DATE_FORMAT).to_string();
        let result = sqlx::query(sql)
            .bind(self.org_name)
            .bind(create_time)
            .bind(self.remark)
            .execute(&mut *db)
            .await?;
        Ok(result)
    }
}
